package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.model.ArticleLikes;
import com.myblog.model.CommentLikes;
import com.myblog.model.LeaveLikes;
import com.myblog.redis.RedisService;
import com.myblog.service.LikeService;
import com.myblog.service.UserService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import com.myblog.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Slf4j
@RestController
public class LikeController {

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private RedisService redisService;

    /**
     * 获取点赞列表
     * @param pageNum
     * @param rows
     * @return
     */
    @PostMapping("/getArticleThumbsUp")
    public String getArticleLikes(@Param(value = "pageNum")Integer pageNum,@Param(value = "rows") Integer rows){
        try{
            //调用service层的查询点赞管理
            DataMap data = likeService.getArticleLikesByPage(rows,pageNum);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController getArticleThumbsUp Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    @GetMapping("/readAllThumbsUp")
    public String readAllLikes(){
        try{
            //调用service层的查询点赞管理
            DataMap data = likeService.setAllLikesIsRead();

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController readAllThumbsUp Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 文章点赞
     * @param articleId
     * @return
     */
    @GetMapping("/addArticleLike")
    public String addArticleLike(@RequestParam(value = "articleId")String articleId, HttpServletRequest request){
        try{
            //获取点赞人
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            String userName = userPrincipal.getName();
            //判断当前是否已经点赞，如果已经点赞了，返回fail
            if(likeService.isLike(Long.parseLong(articleId),userName)){
                return JsonResult.fail(CodeType.ARTICLE_HAS_THUMBS_UP).toJSON();
            }
            //点赞，更新点赞数量
            DataMap data = likeService.updateLikeByArticleId(Long.parseLong(articleId));
            ArticleLikes likeOfArticle = new ArticleLikes(Long.parseLong(articleId),userService.searchUserByName(userName).getId(),new TimeUtil().getFormatDateForSix());
            likeService.insertArticleLikesRecords(likeOfArticle);
            //更新redis中的数据
            redisService.updateThumbsUpOnRedis(1);
            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController addArticleLike Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    //addLeaveMessageLike?pageName=categories&respondentId=p48

    /**
     * 点赞留言
     * @param pageName
     * @param leaveIdString
     * @param request
     * @return
     */
    @GetMapping("/addLeaveMessageLike")
    public String addLeaveLike(@RequestParam(value = "pageName")String pageName,
                               @RequestParam(value = "respondentId")String leaveIdString,
                               HttpServletRequest request){
        try{
            //获取点赞人
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            String userName = userPrincipal.getName();
            //处理leaveIdString
            String s = leaveIdString.substring(1,leaveIdString.length());
            Integer leaveId = Integer.parseInt(s);
            //判断当前是否已经点赞，如果已经点赞了，返回fail
            if(likeService.leaveIsLike(pageName,leaveId,userName)){
                return JsonResult.fail(CodeType.MESSAGE_HAS_THUMBS_UP).toJSON();
            }
            //点赞，更新点赞数量
            DataMap data = likeService.updateLeaveLike(leaveId);
            LeaveLikes likeOfLeave = new LeaveLikes(pageName,leaveId,userService.searchUserByName(userName).getId(),new TimeUtil().getFormatDateForSix());
            likeService.insertLeaveLikesRecords(likeOfLeave);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController addLeaveLike Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    //addCommentLike?articleId=1678597377&respondentId=p18

    /**
     * 点赞评论
     * @param articleIdString
     * @param commentIdString
     * @param request
     * @return
     */
    @GetMapping("/addCommentLike")
    public String addCommentLike(@RequestParam(value = "articleId")String articleIdString,
                               @RequestParam(value = "respondentId")String commentIdString,
                               HttpServletRequest request){
        try{
            //获取点赞人
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            String userName = userPrincipal.getName();
            //处理leaveIdString
            String s = commentIdString.substring(1,commentIdString.length());
            Integer commentId = Integer.parseInt(s);
            //处理articleId
            long articleId = Long.parseLong(articleIdString);
            //判断当前是否已经点赞，如果已经点赞了，返回fail
            if(likeService.commentIsLike(articleId,commentId,userName)){
                return JsonResult.fail(CodeType.MESSAGE_HAS_THUMBS_UP).toJSON();
            }
            //点赞，更新点赞数量
            DataMap data = likeService.updateCommentLike(commentId);
            CommentLikes likeOfComment = new CommentLikes(articleId,commentId,userService.searchUserByName(userName).getId(),new TimeUtil().getFormatDateForSix());
            likeService.insertCommentLikesRecords(likeOfComment);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController addCommentLike Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
